﻿using UnityEngine;
using UnityEngine.InputSystem.XR;

public class PlayerWallHFSM : PlayerBaseHFSM {
	public PlayerWallHFSM(PlayerController controller, PlayerStateEnum stateName, State<PlayerStateEnum> superFSM, StateFactory<PlayerStateEnum> factory) : base(controller, stateName, superFSM, factory) {
		//AddConditionToSelf(() => _controller.IsTouchingEdge || _controller.IsTouchingWall);
		AddConditionToSelf(() => _controller.IsTouchingWall);
		//删除爬墙
		AddNewTransition(PlayerStateEnum.GroundHFSM, () => _controller.IsOnGround);
		AddNewTransition(PlayerStateEnum.AirHFSM, () =>
			  !(_controller.IsTouchingEdge || _controller.IsTouchingWall || _controller.IsOnGround)
			  || _controller.Jump || SlideVelocityExceedMax()
		);

	}

	public override void Enter() {
		base.Enter();
		_controller.StateData.CanChangeDir = false;
		_controller.SetGravity(0);
	}
	public override void Update() {
		base.Update();
		var tmp = _controller.Velocity;
		_controller.Velocity = new UnityEngine.Vector2(0, tmp.y);//防止x方向的微小速度
	}

	public override void Exit() {
		base.Exit();
		_controller.StateData.CanChangeDir = true;
		_controller.ResetGravity();
	}
	bool SlideVelocityExceedMax() {
		//Debug.Log($"{_controller.Velocity.y}");
		return _controller.Velocity.y < _controller.ConfigData.MaxVelocityFallFromWall;
	}
	//bool SlideVelocityExceedMax() => _controller.Velocity.y < _controller.ConfigData.MaxVelocityFallFromWall;
}